<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" v-if="!isComponent">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['project:project:add']"
          >添加项目</el-button
        >
      </el-col>
    </el-row>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item>
        <el-input
          v-model="queryParams.projectName"
          placeholder="项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="queryParams.groupId"
          placeholder="请选择客户"
          clearable
        >
          <el-option
            v-for="dict in dict.type.ly_group"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="projectList"
      :highlight-current-row="isComponent"
      @current-change="handleCurrentChange"
    >
      <el-table-column label="项目id" align="center" prop="id" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="项目地址" align="center" prop="projectAddress" />
      <el-table-column label="所属客户" align="center" prop="groupId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ly_group" :value="scope.row.groupId" />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="serverName" />
      <el-table-column label="创建时间" align="center" prop="ctime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        v-if="!isComponent"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateOrDetail(scope.row, '修改项目')"
            v-hasPermi="['project:project:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdateOrDetail(scope.row, '项目详情')"
            v-hasPermi="['project:project:detail']"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:project:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目对话框 -->
    <el-dialog
      :fullscreen="true"
      :center="true"
      :title="title"
      :visible.sync="open"
      append-to-body
      destroy-on-close
    >
      <div class="dialog-content">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="项目名称：" prop="projectName">
            <el-input
              v-model="form.projectName"
              placeholder="请输入项目名称"
              style="width: 217px"
              :readonly="title === '项目详情'"
            />
          </el-form-item>
          <el-form-item label="所属客户">
            <el-select v-model="form.groupId" :disabled="title === '项目详情'">
              <el-option
                v-for="dict in dict.type.ly_group"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目经度：" prop="projectGpsx">
                <el-input
                  v-model="form.projectGpsx"
                  placeholder="请输入项目经度"
                  :readonly="title === '项目详情'"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目纬度：" prop="projectGpsy">
                <el-input
                  v-model="form.projectGpsy"
                  placeholder="请输入项目纬度"
                  :readonly="title === '项目详情'"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="项目地址：" prop="projectAddress">
            <el-input
              v-model="form.projectAddress"
              placeholder="请输入项目地址"
              :readonly="title === '项目详情'"
            />
          </el-form-item>
          <el-form-item label="简介">
            <editor v-model="form.content" :min-height="192" />
          </el-form-item>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer" v-if="!isComponent">
        <el-button
          type="primary"
          @click="submitForm"
          v-show="title !== '项目详情'"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProject,
  getProject,
  delProject,
  addProject,
  updateProject,
} from "@/api/project/project";

export default {
  name: "Project",
  // 数据字典
  dicts: ["ly_group"],
  props: {
    isComponent: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // [项目] 项目表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 当前选中行项目
      currentProject: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        groupId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
        ],
        projectAddress: [
          { required: true, message: "项目地址不能为空", trigger: "blur" },
        ],
        projectGpsx: [
          { required: true, message: "项目经度不能为空", trigger: "blur" },
        ],
        projectGpsy: [
          { required: true, message: "项目纬度不能为空", trigger: "blur" },
        ],
        content: [{ required: true, message: "简介不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then((response) => {
        this.projectList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectName: null,
        groupId: null,
        projectGpsx: null,
        projectGpsy: null,
        projectAddress: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        groupId: null,
      };
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.title = "添加项目";
      this.open = true;
    },
    /** 修改/详情按钮操作 */
    handleUpdateOrDetail(row, title) {
      this.reset();
      const id = row.id;
      getProject(id).then((response) => {
        this.form = response.data;
        this.title = title;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id !== null) {
            updateProject(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProject(this.form).then((response) => {
              console.log("this.form", this.form);
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal
        .confirm('是否确认删除[项目] 项目编号为"' + id + '"的数据项？')
        .then(function () {
          return delProject(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 表格选中事件 */
    handleCurrentChange(val) {
      if (!this.isComponent) {
        return;
      }
      this.currentProject = val;
    },
    /** 获取当前选中行id */
    getCurrentProject() {
      return this.currentProject;
    },
  },
};
</script>

<style lang="scss" scoped>
.dialog-content {
  width: 60%;
}
</style>
